﻿#include "IniSection.h"




#pragma warning(push) 
#pragma warning( disable : 4290 )
#pragma warning( disable : 4996 )


namespace ksini 
{
 

//===========================================
//	IniSection
//===========================================

IniSection::IniSection(const std::string& sname, 
		const std::string& commentLine)	  throw(std::runtime_error)  
					   : m_sname(sname), m_sCommentLine(commentLine) 
{
	// check comment line
	for(size_t c=0; c<m_sCommentLine.length(); c++)
	{
		if( m_sCommentLine[c] == '\n' )
		{
			throw std::runtime_error("bad comment line");
		}
	}
}


//===========================================
IniKeyValue*  IniSection::addKeyValue(const IniKeyValue& kv) 
{
	int keyindex = 0-1;

	if( searchKey(keyindex, kv.get_key() ) )
	{
		m_vector_keyval[keyindex].set_value( kv.get_value() );

		if(kv.get_comment_line().  length())
		{
		   m_vector_keyval[keyindex].set_comment_line(  kv.get_comment_line()  );
		}

		return  &m_vector_keyval.at( keyindex );
	}
 

	m_vector_keyval.push_back(kv); 
	return  &m_vector_keyval.at( m_vector_keyval.size()-1 );
}

//===========================================================
IniKeyValue* IniSection::addKeyValue(const std::string& skey, const std::string& svalue, const std::string& sComment) 
{
	IniKeyValue kv(skey, svalue, sComment);
	return addKeyValue(kv);		
}

//===========================================================
bool  IniSection::searchKey(int& index , const std::string& sKey) const 
{
	index = -1;
	if(m_vector_keyval.size() == 0) return false;

	for(int c=0; c<(int)m_vector_keyval.size(); c++)
	{
		const IniKeyValue& kv = m_vector_keyval.at(c);
		if(kv.get_key() == sKey)   
		{
			// found !
			index = c;
			return true;
		}

	}

	return false;	
}


//================================================================
bool  IniSection::isKeyExests(const std::string& sKey) 
{
	int indx = -1;

	if( !searchKey(indx, sKey) ) 
	{
		return false;
	}

	return true;	
}

//===================================================
IniKeyValue&  IniSection::getKeyValueByIndex(const int index) throw(std::runtime_error&) 
{
	if( (index >= (int)m_vector_keyval.size() )  || (index<0) ) 
	{
		// error
		throw::std::runtime_error("Invalid index");
		static int INVALID =0;
		return (IniKeyValue&)INVALID;
	}

	IniKeyValue& res = m_vector_keyval.at(index);
	return res;	
}

//================================================================
const  IniKeyValue&  IniSection::getKeyValueByIndex (const int index) const throw(std::runtime_error&)
{

	if( (index >= (int)m_vector_keyval.size()) || (index<0) ) 
	{
		// error
		throw::std::runtime_error("Invalid index");
		static int INVALID =0;
		return (const  IniKeyValue&)INVALID;
	}

	const  IniKeyValue& res = m_vector_keyval.at(index);

	return res;	
}

//=====================================================================
void IniSection::make_keyvalue_text(std::string& dest, bool includeComments) const
{
	dest = "";
	if(m_vector_keyval.size() == 0) return ;

	for(size_t c=0; c<m_vector_keyval.size(); c++)
	{
		const IniKeyValue& curr = m_vector_keyval.at(c);
		if(includeComments)
		{
			if(curr.get_comment_line() .length())
			{
				dest += "; ";
				dest += curr.get_comment_line();
				dest += "\n";
			}
		}

		dest += curr.get_key();
		dest += "=";
		dest += curr.get_value();
		dest += "\n";
	}
}

//=======================================================================
void IniSection::make_full_inisection_text(std::string& dest) const
{
	dest = "";

	if(m_sCommentLine.length())
	{
		dest += "; ";
		dest += m_sCommentLine;
		dest += "\n";
	}

	dest += "[";
	dest += m_sname;
	dest +="]";
	dest += "\n";

	for(size_t c=0; c<m_vector_keyval.size(); c++)
	{
		const IniKeyValue& curr = m_vector_keyval.at(c);

		if(curr.get_comment_line().length())
		{
			dest += "; ";
			dest += curr.get_comment_line();
			dest += "\n";
		}


		dest += curr.get_key();
		dest += "=";
		dest += curr.get_value();
		dest += "\n";
	}
}


}

#pragma warning(pop)

